function display_structure(structure, start_indent_size, step_size)
%display_structure(structure)
%This function displays the fields
%in a structure. This is done using
%the disp function.
%Fields in structures within the structure
%are shown indented.
%Input:     -Structure
%           -Integer, the size of the first indent
%           -Integer, the size of the indent
%Mark Jobse 23-07-2002
%14-08-2002 Added class and size info

if (nargin == 2)
    step_size = 3;
else
end

if (nargin == 1)
    start_indent_size = 1;
    step_size = 3;
else
end

if (start_indent_size < step_size) %First put the name of the structure
    name_string = inputname(1);
    disp(name_string);
    start_indent_size = start_indent_size + step_size;
else
end

start_indent(1:start_indent_size) = ' ';
new_start_indent_size = start_indent_size + step_size;
indent = start_indent;

%Declarations
structure_out = [];
struct_cell = [];
non_struct_cell = [];

%Get all fields out of the structure
cell = fieldnames(structure);

%Now split those fields into structures and other fields
counter = 1;
counter2 = 1;
for i = 1:length(cell)
    boolean = isstruct(getfield(structure, cell{i}));
    if (boolean)
        struct_cell{counter} = cell{i};
        counter = counter + 1;
    else
        non_struct_cell{counter2} = cell{i};
        counter2 = counter2 + 1;
    end
end

%First display the structure fields
for i = 1:length(struct_cell)
    new_structure = getfield(structure, struct_cell{i});
    string = (struct_cell{i});
    string = append(indent, string);
    disp(string);
    display_structure(new_structure, new_start_indent_size, step_size);
end

%Now display the names of the non-structure fields
for i = 1:length(non_struct_cell);
    string = non_struct_cell{i};
    string = append(indent, string);
    class_string = class(getfield(structure, non_struct_cell{i}));
    size_string = num2str(size(getfield(structure, non_struct_cell{i})));
    %Replace the spaces in the string with x-es
    pos = find(size_string == ' ');
    pos = split_array(pos);
    size_string(pos(:,1)) = 'x';
    pos(:,1) = [];
    zeropos = find(pos == 0); %Detect zeros in the matrix created by split_array
    pos(zeropos) = []; %Remove them
    size_string(pos) = [];
    string = append(string, ': ');
    string = append(string, '[');
    string = append(string, size_string);
    string = append(string, ' ');
    string = append(string, class_string);
    string = append(string, ']');
    disp(string);
end

function matrix = split_array(array)
%This function splits an array into a number of colums. 
%This by creating a new row every time there is a large step in
%the values in the array. This function assumes the values to be sorted!
%Input:     -Array containing SORTED (intensity) values
%Output:    -Matrix created from the original array
array = double(array);
lengte = length(array);
previous_value = array(1);
teller2 = 1;
teller3 = 1;
for teller = 1:lengte
    if ((array(teller) == previous_value) | (array(teller) == (previous_value + 1)))
        matrix(teller3, teller2)=array(teller);
        teller = teller + 1;
        teller2 = teller2 + 1;
    else
        matrix(teller3 + 1, 1)=array(teller);
        teller = teller + 1;
        teller2 = 2;
        teller3 = teller3 + 1;
    end
    previous_value = array(teller - 1);
end
%matrix = unint8(matrix);

function array_out = append(array_in, array_to_append)
%This function appends an array to the tail of another array.
%Input:     -Array, the original array
%           -Array, the array that will be attached to array_in
%Output:    -Array

length_array_in = length(array_in);
length_array_to_append = length(array_to_append);
array_out = array_in;
array_out((length_array_in + 1):(length_array_in + length_array_to_append)) = array_to_append;
